<?php
/**
 * ClassName:货代处理器
 * @auditor:leijunxiang
 * @time 2025/4/1 15:17
 */

namespace app\controller\freightForward;

use app\BaseController;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\tool\freightForward\FreightForwardHandle;

class FreightForwardController extends BaseController
{
    /**
     * 新增货代信息
     * @return string
     */
    public function addFreightForward()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::MODEL_REQUEST_AGREEMENT);
        $content = $requestAgreement->getContent();
        $name = isset($content['name']) ? $content['name'] : '';
        if (empty($name)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入货代名称！");
            return;
        }
        $handle = app(FreightForwardHandle::class);
        //(true/false)开始新增(货代名称)
        $flag = $handle->startAdd($name);
        if (!$flag) {
            $responseObj->setErrCode($handle->getErrCode());
            $responseObj->setErrMsg($handle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($flag);
        }
    }

    /**
     * 查询货代信息
     * @return void
     */
    public function select()
    {
        $handle = app(FreightForwardHandle::class);
        $responseObj = app(ResponseAgreement::class);
        //(数据)开始查询
        $data = $handle->startSelect();
        if (empty($data)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("没有查询到数据！");
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($data);
        }
        return $data;
    }

    /**
     * 删除货代信息
     * @param $forwardId
     * @return void
     */
    public function del()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestObj = app(RequestAgreement::class);
        $attr = $requestObj->getContent()['forwardIdAttr'];
        if (empty($attr)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入货代id！");
            return;
        }
        $handle = app(FreightForwardHandle::class);
        //(true/false)开始删除(货代id数组)
        $flag = $handle->startDel($attr);
        if (!$flag) {
            $responseObj->setErrCode($handle->getErrCode());
            $responseObj->setErrMsg($handle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        }
    }

    /**
     * 修改货代信息
     * @return void
     */
    public function update()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::MODEL_REQUEST_AGREEMENT);
        $content = $requestAgreement->getContent();
        $forwardId = isset($content['forwardId']) ? $content['forwardId'] : '';
        if (empty($forwardId)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入货代id！");
            return;
        }
        $name = isset($content['name']) ? $content['name'] : '';
        if (empty($name)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入货代名称！");
            return;
        }
        $handle = app(FreightForwardHandle::class);
        //(true/false)开始修改(货代id,货代名称)
        $flag = $handle->startUpdate($forwardId,$name);
        if (!$flag) {
            $responseObj->setErrCode($handle->getErrCode());
            $responseObj->setErrMsg($handle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        }
    }


}